from fastapi import APIRouter,Form              # form表单
from fastapi.responses import FileResponse      # 静态文件
#from fastapi.templating import Jinja2Templates  # jinja2方法
from pydantic import BaseModel,Field      # 校验模型,类型内容规则限制
from enum import Enum               # 枚举类型
from datetime import date           # 时间类型
from typing import Union, List, Optional
import os

router_mycash = APIRouter()
#templates = Jinja2Templates(directory="templates")      # 实例化Jinja2对象
# 定义枚举类型
class Enum_type(Enum):
    add_money = 0       # 收入
    del_money = 1       # 支出

# 定义存储类型模型
class insert_type(BaseModel):
    name: str               # 名称
    money: float=Field(default=0,gt=0,lt=100)            # 金钱,判断存储范围
    type: Enum_type         # 收支
    time: date              # 时间

# 打开流水写入页面
@router_mycash.get("/login")
async def read_root():
    base_path = os.path.dirname(os.path.abspath(__file__))
    file_path = os.path.join(base_path, "templates", "insert_money.html")
    return FileResponse(file_path)

# 记录接口
@router_mycash.post('/add')
async def add():
    return {"记录成功"}

# async def add(data: insert_type):       # 传入的值必须符合这个模型要求
#     return data

@router_mycash.post("/regin")
def regin(username: str = Form(..., max_length=16, min_length=8, regex='[a-zA-Z]'),
          password: str = Form(..., max_length=16, min_length=8, regex='[0-9]')):
    print(f"username:{username},password:{password}")
    return {"username": username}
# @router_mycash.get('/')
# async def indexindex():
#     return {"message": "Hello study"}